#!/bin/bash

alias logger="/usr/bin/logger -is -plocal7.info -tpowerdown" 

if [ ${DEBUG:=0} -gt 0 ]
   then set -x -v
fi

if [ -z "${1}" ]
   then OPT="-h"
   else OPT="${1}"
fi

logger "Powerdown initiated"

if [ -f /var/run/powerdown.pid ]
   then logger "Powerdown already active, this one is exiting"
	exit
   else echo $$ > /var/run/powerdown.pid
fi

trap "rm -f /var/run/powerdown.pid" EXIT HUP INT QUIT

/etc/rc.d/rc.unRAID stop

# /sbin/poweroff
logger "Initiating Shutdown with ${1}"
/sbin/shutdown -t5 ${OPT} now
